added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSRunProcessAsUser / ReadMe.txt
blob5fff1a03061f937fa09b0596a527a9fb91403f78
1 ======================================================================================
2               Windows APPLICATION: CSRunProcessAsUser Overview                        
3 ======================================================================================
5 //////////////////////////////////////////////////////////////////////////////////////
6 Summary:
8 The code sample demonstrates how to run a process as a different user. 
11 //////////////////////////////////////////////////////////////////////////////////////
12 Demo:
14 Step1. Build this project in Visual Studio 2010. 
16 Step2. Run CSRunProcessAsUser.exe.
18 Step3. Fill out the User Name, Domain (if it is an Active Directory user account), and 
19        Password textboxes for the user that you want to run as.  Alternatively, you can 
20        click the "..." button next to the User Name textbox. It will prompt a standard 
21        user credential collection dialog.  You can fill out the user name and password 
22        in it too.
23        
24 Step4. Click the "Command..." button and select the program that you want to run as the 
25        specified user in Step 3.
27 Step5. Click the "Run Command" button to run the program as the specified user.  When 
28        the process is started successfully, you will see a message box saying "the 
29        process xxx started".  You can verify that the process is run as the specified 
30        user in Task Manager.  When you exit the new process, you will see another 
31        message box saying "the process xxx exited".
34 //////////////////////////////////////////////////////////////////////////////////////
35 Implementation:
37 1. The sample uses the "Process.Start" function to implement running programs with 
38    different users. 
40             try
41             {
42                 // Check the parameters.
43                 if (!string.IsNullOrEmpty(tbUserName.Text) &&
44                     !string.IsNullOrEmpty(tbPassword.Text) &&
45                     !string.IsNullOrEmpty(tbCommand.Text))
46                 {
47                     SecureString password = StringToSecureString(tbPassword.Text);
48                     Process proc = Process.Start(
49                         this.tbCommand.Text,
50                         this.tbUserName.Text,
51                         password,
52                         this.tbDomain.Text);
54                     ProcessStarted(proc.Id);
56                     proc.EnableRaisingEvents = true;
57                     proc.Exited += new EventHandler(ProcessExited);
58                 }
59                 else
60                 {
61                     MessageBox.Show("Please fill in the user name, password and command");
62                 }
63             }
64             catch (Win32Exception w32e)
65             {
66                 ProcessStartFailed(w32e.Message);
67             }
68    
69 2. The sample uses the C++/CLI library "Kerr.Credentials" that wraps the call of the 
70    native API CredUIPromptForCredentials to gather the user credential. The 
71    Kerr.Credentials library provided by Kenny Kerr is downloaded from this MSDN 
72    article: http://www.microsoft.com/indonesia/msdn/credmgmt.aspx.
73    
74             try
75             {
76                 using (Kerr.PromptForCredential dialog = new Kerr.PromptForCredential())
77                 {
78                     dialog.Title = "Please specify the user";
79                     dialog.DoNotPersist = true;
80                     dialog.ShowSaveCheckBox = false;
81                     dialog.TargetName = Environment.MachineName;
82                     dialog.ExpectConfirmation = true;
84                     if (DialogResult.OK == dialog.ShowDialog(this))
85                     {
86                         tbPassword.Text = SecureStringToString(dialog.Password);
87                         string[] strSplit = dialog.UserName.Split('\\');
88                         if (strSplit.Length == 2)
89                         {
90                             this.tbUserName.Text = strSplit[1];
91                             this.tbDomain.Text = strSplit[0];
92                         }
93                         else
94                         {
95                             this.tbUserName.Text = dialog.UserName;
96                         }
97                     }
98                 }
99             }
100             catch (Exception ex)
101             {
102                 MessageBox.Show(ex.ToString(), "Error");
103             }
105 3. After the new process is started, we register its Exited event so that we can be 
106    notified when the process exits.
108         proc.EnableRaisingEvents = true;
109         proc.Exited += new EventHandler(ProcessExited);
111         /// <summary>
112         /// Triggered when the target process is exited.
113         /// </summary>
114         private void ProcessExited(object sender, EventArgs e)
115         {
116             Process proc = sender as Process;
117             if (proc != null)
118             {
119                 MessageBox.Show("Process " + proc.Id.ToString() + " exited");
120             }
121         }
124 //////////////////////////////////////////////////////////////////////////////////////
125 References:
127 Process.Start
128 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx
130 Credential Management with the .NET Framework 2.0
131 http://www.microsoft.com/indonesia/msdn/credmgmt.aspx
134 //////////////////////////////////////////////////////////////////////////////////////